use crate::Segment::Segment;

pub struct Segments {
    _segs: Vec<Segment>,
}

impl Segments {
    pub fn new(ps: Vec<Segment>) -> Self {
        Self { _segs: ps }
    }

    /**
     * 多端线段减去另一条线段
     */
    pub fn subtract(&self, other: &Segment, ep: Option<f64>) -> Vec<Segment> {
        let mut remainder_edges = Vec::new();
        for edge in &self._segs {
            if edge.is_zero(ep) {
                break;
            }

            let dif = edge.subtract(other, ep);
            if dif.len() > 0 {
                for item in dif {
                    if !item.is_zero(ep) {
                        remainder_edges.push(item);
                    }
                }
            }
        }
        remainder_edges
    }

    /**
     * 多端线段减去多条线段
     */
    pub fn subtract_segs(&self, other_edgess: &Vec<Segment>, ep: Option<f64>) -> Vec<Segment> {
        let mut remainder_edges = Vec::new();
        for edge in &self._segs {
            if edge.is_zero(ep) {
                break;
            }

            let mut difference = vec![edge.clone()];
            for other in other_edgess {
                if difference.len() == 0 {
                    break;
                }

                let mut result = Vec::new();
                for seg in &difference {
                    let dif = seg.subtract(other, ep);
                    if dif.len() > 0 {
                        for item in dif {
                            if !item.is_zero(ep) {
                                result.push(item);
                            }
                        }
                    }
                }
                difference = result;
            }

            if difference.len() > 0 {
                remainder_edges.append(&mut difference);
            }
        }
        remainder_edges
    }
}
